const Mock = require('mockjs')

var data = Mock.mock({
  items: [
    {
      name: '张三',
      sex: 'male',
      birthday: '@date'
    },
    {
      name: '李四',
      sex: 'male',
      birthday: '@date'
    },
    {
      name: '王五',
      sex: 'male',
      birthday: '@date'
    },
    {
      name: '赵六',
      sex: 'male',
      birthday: '@date'
    },
    {
      name: '翠花',
      sex: 'female',
      birthday: '@date'
    },
    {
      name: '二狗',
      sex: 'female',
      birthday: '@date'
    },
    {
      name: '老六',
      sex: 'female',
      birthday: '@date'
    }
  ]
})

module.exports = [
  {
    url: '/vue-admin-template/employee/list',
    type: 'get',
    response: (config) => {
      const params = config.query
      let datas = []
      if (params.name === '') {
        datas = data.items
      } else {
        datas = data.items.filter((item) => item.name.includes(params.name))
      }
      return {
        code: 20000,
        data: {
          total: datas.length,
          items: datas
        }
      }
    }
  },

  {
    url: '/vue-admin-template/employee/add',
    type: 'post',
    response: (config) => {
      const body = config.body
      console.log(body)
      const len = data.items.length
      data.items.splice(len, 0, body)
      return {
        code: 20000,
        msg: '成功'
      }
    }
  },

  {
    url: '/vue-admin-template/employee/delete',
    type: 'post',
    response: (config) => {
      const body = config.body
      console.log(body)
      for (let index = 0; index < data.items.length; index++) {
        for (const item of body) {
          if (item.name === data.items[index].name) {
            data.items.splice(index, 1)
          }
        }
      }
      return {
        code: 20000,
        msg: '成功'
      }
    }
  }
]
